!pip install pandas==1.3.3
!pip install requests==2.26.0
!pip install bs4==4.10.0 -y
!pip install html5lib==1.1 -y
!pip install lxml==4.6.4
!pip install plotly==5.3.1
Collecting pandas==1.3.3
Downloading pandas-1.3.3-cp39-cp39-macosx_10_9_x86_64.whl (11.6 MB)
|████████████████████████████████| 11.6 MB 202 kB/s eta 0:00:01
Requirement already satisfied: pytz>=2017.3 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from pandas==1.3.3) (2022.1)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from pandas==1.3.3) (2.8.2)
Requirement already satisfied: numpy>=1.17.3 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from pandas==1.3.3) (1.22.4)
Requirement already satisfied: six>=1.5 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas==1.3.3) (1.16.0)
Installing collected packages: pandas
Attempting uninstall: pandas
Found existing installation: pandas 1.4.2
Uninstalling pandas-1.4.2:
Successfully uninstalled pandas-1.4.2
Successfully installed pandas-1.3.3
Collecting requests==2.26.0
Downloading requests-2.26.0-py2.py3-none-any.whl (62 kB)
|████████████████████████████████| 62 kB 668 kB/s eta 0:00:011
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests==2.26.0) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests==2.26.0) (1.26.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests==2.26.0) (2021.10.8)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests==2.26.0) (3.3)
Installing collected packages: requests
Attempting uninstall: requests
Found existing installation: requests 2.27.1
Uninstalling requests-2.27.1:
Successfully uninstalled requests-2.27.1
Successfully installed requests-2.26.0
Usage:
pip install [options] <requirement specifier> [package-index-options] ...
pip install [options] -r <requirements file> [package-index-options] ...
pip install [options] [-e] <vcs project url> ...
pip install [options] [-e] <local project path> ...
pip install [options] <archive url/path> ...
no such option: -y
Usage:
pip install [options] <requirement specifier> [package-index-options] ...
pip install [options] -r <requirements file> [package-index-options] ...
pip install [options] [-e] <vcs project url> ...
pip install [options] [-e] <local project path> ...
pip install [options] <archive url/path> ...
no such option: -y
Collecting lxml==4.6.4
Downloading lxml-4.6.4-cp39-cp39-macosx_10_14_x86_64.whl (4.5 MB)
|████████████████████████████████| 4.5 MB 276 kB/s eta 0:00:01
Installing collected packages: lxml
Attempting uninstall: lxml
Found existing installation: lxml 4.8.0
Uninstalling lxml-4.8.0:
Successfully uninstalled lxml-4.8.0
Successfully installed lxml-4.6.4
Collecting plotly==5.3.1
Downloading plotly-5.3.1-py2.py3-none-any.whl (23.9 MB)
|████████████████████████████████| 23.9 MB 2.3 MB/s eta 0:00:01
Requirement already satisfied: tenacity>=6.2.0 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from plotly==5.3.1) (8.0.1)
Requirement already satisfied: six in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from plotly==5.3.1) (1.16.0)
Installing collected packages: plotly
Attempting uninstall: plotly
Found existing installation: plotly 5.8.2
Uninstalling plotly-5.8.2:
Successfully uninstalled plotly-5.8.2
Successfully installed plotly-5.3.1
!pip install yfinance
Collecting yfinance Downloading yfinance-0.1.72-py2.py3-none-any.whl (27 kB) Requirement already satisfied: numpy>=1.15 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from yfinance) (1.22.4) Collecting multitasking>=0.0.7 Downloading multitasking-0.0.10.tar.gz (8.2 kB) Requirement already satisfied: requests>=2.26 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from yfinance) (2.26.0) Requirement already satisfied: pandas>=0.24.0 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from yfinance) (1.3.3) Requirement already satisfied: lxml>=4.5.1 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from yfinance) (4.6.4) Requirement already satisfied: pytz>=2017.3 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from pandas>=0.24.0->yfinance) (2022.1) Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from pandas>=0.24.0->yfinance) (2.8.2) Requirement already satisfied: six>=1.5 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.16.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests>=2.26->yfinance) (2021.10.8) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests>=2.26->yfinance) (1.26.7) Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests>=2.26->yfinance) (2.0.4) Requirement already satisfied: idna<4,>=2.5 in /usr/local/Caskroom/miniconda/base/lib/python3.9/site-packages (from requests>=2.26->yfinance) (3.3) Building wheels for collected packages: multitasking Building wheel for multitasking (setup.py) ... done Created wheel for multitasking: filename=multitasking-0.0.10-py3-none-any.whl size=8500 sha256=25b00487b7c784844582ae6302f7f87dd82c04b28b6c51e923853bee94d774a8 Stored in directory: /Users/ducnga/Library/Caches/pip/wheels/f2/b5/2c/59ba95dcf854e542944c75fe3da584e4e3833b319735a0546c Successfully built multitasking Installing collected packages: multitasking, yfinance Successfully installed multitasking-0.0.10 yfinance-0.1.72
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
tsla = yf.Ticker('TSLA')
tsla_info = tsla.info
tsla_price = tsla.history(period='max')
tsla_price.reset_index(inplace=True)
tsla_price.head
<bound method NDFrame.head of Date Open High Low Close Volume \
0 2010-06-29 3.800000 5.000000 3.508000 4.778000 93831500
1 2010-06-30 5.158000 6.084000 4.660000 4.766000 85935500
2 2010-07-01 5.000000 5.184000 4.054000 4.392000 41094000
3 2010-07-02 4.600000 4.620000 3.742000 3.840000 25699000
4 2010-07-06 4.000000 4.000000 3.166000 3.222000 34334500
... ... ... ... ... ... ...
3010 2022-06-13 669.500000 679.900024 644.049988 647.210022 34255800
3011 2022-06-14 654.859985 678.989990 635.210022 662.669983 32662900
3012 2022-06-15 662.750000 706.989990 654.450012 699.000000 39710600
3013 2022-06-16 668.210022 675.500000 626.080017 639.299988 35796900
3014 2022-06-17 640.299988 662.909973 639.590027 650.280029 30810900
Dividends Stock Splits
0 0 0.0
1 0 0.0
2 0 0.0
3 0 0.0
4 0 0.0
... ... ...
3010 0 0.0
3011 0 0.0
3012 0 0.0
3013 0 0.0
3014 0 0.0
[3015 rows x 8 columns]>
url = 'https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue'
r = requests.get(url).text
tsla_soup = BeautifulSoup(r,'html.parser')
tesla_revenue = pd.DataFrame(columns=['Date', 'Revenue'])
rows = tsla_soup.find_all('tbody')[1].find_all('td')
tsla_date = []
tsla_rev =[]
for i, row in enumerate(rows):
if i % 2 == 0:
tsla_date.append(row.text)
else:
tsla_rev.append(row.text)
tesla_revenue['Date'] = tsla_date
tesla_revenue['Revenue'] = tsla_rev
tesla_revenue
| Date | Revenue | |
|---|---|---|
| 0 | 2022-03-31 | $18,756 |
| 1 | 2021-12-31 | $17,719 |
| 2 | 2021-09-30 | $13,757 |
| 3 | 2021-06-30 | $11,958 |
| 4 | 2021-03-31 | $10,389 |
| 5 | 2020-12-31 | $10,744 |
| 6 | 2020-09-30 | $8,771 |
| 7 | 2020-06-30 | $6,036 |
| 8 | 2020-03-31 | $5,985 |
| 9 | 2019-12-31 | $7,384 |
| 10 | 2019-09-30 | $6,303 |
| 11 | 2019-06-30 | $6,350 |
| 12 | 2019-03-31 | $4,541 |
| 13 | 2018-12-31 | $7,226 |
| 14 | 2018-09-30 | $6,824 |
| 15 | 2018-06-30 | $4,002 |
| 16 | 2018-03-31 | $3,409 |
| 17 | 2017-12-31 | $3,288 |
| 18 | 2017-09-30 | $2,985 |
| 19 | 2017-06-30 | $2,790 |
| 20 | 2017-03-31 | $2,696 |
| 21 | 2016-12-31 | $2,285 |
| 22 | 2016-09-30 | $2,298 |
| 23 | 2016-06-30 | $1,270 |
| 24 | 2016-03-31 | $1,147 |
| 25 | 2015-12-31 | $1,214 |
| 26 | 2015-09-30 | $937 |
| 27 | 2015-06-30 | $955 |
| 28 | 2015-03-31 | $940 |
| 29 | 2014-12-31 | $957 |
| 30 | 2014-09-30 | $852 |
| 31 | 2014-06-30 | $769 |
| 32 | 2014-03-31 | $621 |
| 33 | 2013-12-31 | $615 |
| 34 | 2013-09-30 | $431 |
| 35 | 2013-06-30 | $405 |
| 36 | 2013-03-31 | $562 |
| 37 | 2012-12-31 | $306 |
| 38 | 2012-09-30 | $50 |
| 39 | 2012-06-30 | $27 |
| 40 | 2012-03-31 | $30 |
| 41 | 2011-12-31 | $39 |
| 42 | 2011-09-30 | $58 |
| 43 | 2011-06-30 | $58 |
| 44 | 2011-03-31 | $49 |
| 45 | 2010-12-31 | $36 |
| 46 | 2010-09-30 | $31 |
| 47 | 2010-06-30 | $28 |
| 48 | 2010-03-31 | $21 |
| 49 | 2009-12-31 | |
| 50 | 2009-09-30 | $46 |
| 51 | 2009-06-30 | $27 |
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")
/var/folders/jb/2n_fl39j35g_18r59zrbs9bm0000gn/T/ipykernel_618/349343550.py:1: FutureWarning: The default value of regex will change from True to False in a future version.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
gme = yf.Ticker('GME')
gme_data = gme.history(period='max')
gme_data.reset_index(inplace=True)
gme_data
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 | 6.480513 | 6.773399 | 6.413183 | 6.766665 | 19054000 | 0.0 | 0.0 |
| 1 | 2002-02-14 | 6.850829 | 6.864295 | 6.682504 | 6.733002 | 2755400 | 0.0 | 0.0 |
| 2 | 2002-02-15 | 6.733001 | 6.749833 | 6.632006 | 6.699336 | 2097400 | 0.0 | 0.0 |
| 3 | 2002-02-19 | 6.665671 | 6.665671 | 6.312189 | 6.430017 | 1852600 | 0.0 | 0.0 |
| 4 | 2002-02-20 | 6.463681 | 6.648838 | 6.413183 | 6.648838 | 1723200 | 0.0 | 0.0 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 5118 | 2022-06-13 | 120.510002 | 124.570000 | 114.300003 | 118.250000 | 3448200 | 0.0 | 0.0 |
| 5119 | 2022-06-14 | 117.570000 | 128.000000 | 116.099998 | 126.169998 | 3184800 | 0.0 | 0.0 |
| 5120 | 2022-06-15 | 124.949997 | 131.960007 | 123.639999 | 129.289993 | 2691100 | 0.0 | 0.0 |
| 5121 | 2022-06-16 | 124.940002 | 129.289993 | 120.580002 | 125.730003 | 2498800 | 0.0 | 0.0 |
| 5122 | 2022-06-17 | 126.860001 | 135.860001 | 126.320000 | 135.139999 | 3080200 | 0.0 | 0.0 |
5123 rows × 8 columns
gme_url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html'
r_gme = requests.get(gme_url)
gme_soup = BeautifulSoup(r_gme.text, 'html.parser')
gme_rows = gme_soup.find_all('tbody')[1].find_all('td')
gme_df = pd.DataFrame(columns = ['Date', 'Revenue'])
gme_date = []
gme_rev = []
for i,row in enumerate(gme_rows):
if i % 2 == 0:
# print(row.text)
gme_date.append(row.text)
else:
# print(row.text)
gme_rev.append(row.text)
gme_df['Date'] = gme_date
gme_df['Revenue'] = gme_rev
gme_df
| Date | Revenue | |
|---|---|---|
| 0 | 2020-04-30 | $1,021 |
| 1 | 2020-01-31 | $2,194 |
| 2 | 2019-10-31 | $1,439 |
| 3 | 2019-07-31 | $1,286 |
| 4 | 2019-04-30 | $1,548 |
| ... | ... | ... |
| 57 | 2006-01-31 | $1,667 |
| 58 | 2005-10-31 | $534 |
| 59 | 2005-07-31 | $416 |
| 60 | 2005-04-30 | $475 |
| 61 | 2005-01-31 | $709 |
62 rows × 2 columns
gme_df["Revenue"] = gme_df['Revenue'].str.replace(',|\$',"")
gme_df.dropna(inplace=True)
gme_df = gme_df[gme_df['Revenue'] != ""]
/var/folders/jb/2n_fl39j35g_18r59zrbs9bm0000gn/T/ipykernel_618/2197658718.py:1: FutureWarning: The default value of regex will change from True to False in a future version.
make_graph(tsla_price, tesla_revenue, 'Tesla')
make_graph(gme_data, gme_df, 'GME')